//
//  MaterialClassViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/21.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "MaterialClassViewController.h"


@interface MaterialClassViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>

@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic,retain) NSMutableArray *classArr;//分类 分区数
@property(nonatomic ,retain)NSMutableArray *textArr; //每一个的文字的数组
@property(nonatomic ,retain)NSMutableArray *allTextArr;//所有文字数组
@property(nonatomic, copy)ShipuModel *model;
@property(nonatomic, retain)NSMutableArray *listArr;//中间转换数组
@property(nonatomic, assign)NSInteger index; //记录点击的哪一个item
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic ,retain)UISegmentedControl *seg;
@property(nonatomic, retain)UICollectionView *classCollView;


@end

@implementation MaterialClassViewController
- (void)dealloc
{
   
    
    [_classArr release];
    [_textArr release];
    [_allTextArr release];
    [_model release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //标题
   self.navigationItem.title = self.shi_model.text;
    self.navigationController.navigationBar.translucent = NO;
    
    
    if ([_shi_model.text isEqualToString:@"食材分类"]) {
        
    
        [self dataHandler];
    }
    else if ([_shi_model.text isEqualToString:@"食谱分类"])
    {
        [self dataHandler1];
        
    }
    [self creatBottomView];
    
 [self creatView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
}
-(void)leftAction:(UIBarButtonItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}

//请求数据
-(void)dataHandler
{
    
    NSString *str =@"http://api.izhangchu.com/?methodName=MaterialSubtype&token=0&user_id=0&version=4.1";
       [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr = responseObject[@"data"][@"data"];
        
        self.classArr= [ShipuModel modelHanderWithArray:arr];
        self.allTextArr = [ NSMutableArray array];
        for (NSDictionary *dic1 in arr) {
            //获取第二个data
            NSArray *iArr = dic1[@"data"];
            ShipuModel  *model = [[ShipuModel alloc] init];
            model.textArr = [ NSMutableArray array];
            //把所有的文字存成数组 再添加到大数组里
            for (NSDictionary *dic2 in iArr) {
                [model.textArr addObject:dic2[@"text"]];
            }
            [self.allTextArr addObject: model.textArr];
            
        }
           [self.collectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
   }

-(void)dataHandler1
{
    
    NSString *str =@"http://api.izhangchu.com/?methodName=CategoryIndex&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr = responseObject[@"data"][@"data"];
        
        self.classArr= [ShipuModel modelHanderWithArray:arr];
        self.allTextArr = [ NSMutableArray array];
        for (NSDictionary *dic1 in arr) {
            //获取第二个data
            NSArray *iArr = dic1[@"data"];
            ShipuModel  *model = [[ShipuModel alloc] init];
            model.textArr = [ NSMutableArray array];
            //把所有的文字存成数组 再添加到大数组里
            for (NSDictionary *dic2 in iArr) {
                [model.textArr addObject:dic2[@"text"]];
            }
            [self.allTextArr addObject: model.textArr];
            
        }
              [self.collectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

//导航的segment
-(void)creatBottomView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH*375, 667 *HEIGHT)];
    _scrollView.backgroundColor =[ UIColor redColor];
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(WIDTH*375 *4, 0);
    [_scrollView release];
    //_scrollView.contentOffset = CGPointMake(375 * 4, 0);
    _scrollView.pagingEnabled = NO;
    _scrollView . bounces = NO;
    _scrollView.delegate = self;
}

-(void)creatView
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    //cell大小
    layout.itemSize = CGSizeMake(( WIDTH*375 - 20 *WIDTH) /4 ,HEIGHT * 30);
    //行列间距
    //行
    layout.minimumLineSpacing = 1 *WIDTH;
    //列
    layout.minimumInteritemSpacing = 0;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(0,10 * WIDTH,0,0);
    self.collectionView =[[ UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 *WIDTH, 667 *HEIGHT)collectionViewLayout:layout];
    self.collectionView .delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:self.collectionView];
    //[_collectionView release];
    self.collectionView.pagingEnabled = NO;
    [self.collectionView registerClass:[ClassCollectionViewCell  class] forCellWithReuseIdentifier:@"cell"];
    //头部区域
     [self.collectionView registerClass: [MyHeadCollectionViewCell class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"header"];
    
     layout.headerReferenceSize = CGSizeMake(100,HEIGHT * 50);
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    
    //从重用池获取头部视图
    MyHeadCollectionViewCell *headercell =[ collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    ShipuModel *m = self.classArr[indexPath.section];
    headercell.title.text = m.text;
    
    return headercell;
  }
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return _classArr.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{ //获取对应的每一个数组
    _textArr = _allTextArr[ section];

        return _textArr.count ;
   
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   ClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    _listArr = _allTextArr[indexPath.section];
    cell.name.text = _listArr[indexPath.item];
    
    return cell;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
